import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {Gys} from "../../../../model/admin/Gys";
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {NzModalService} from "ng-zorro-antd/modal";
import {GyseditComponent} from "../gysedit/gysedit.component";

@Component({
  selector: 'app-gyslist',
  templateUrl: './gyslist.component.html',
  styleUrls: ['./gyslist.component.css']
})
export class GyslistComponent implements OnInit {
  gl:Array<Gys>
  constructor(private adminService:AdminService,
              private alertService:AlertService,
              private modal: NzModalService,
              private viewContainerRef: ViewContainerRef) {
    this.gl = new Array<Gys>()
  }

  ngOnInit(): void {
    this.getGyses();
  }
getGyses(){
    this.adminService.getGyses().then((data:any)=>{
      if(data.msg == "ok"){
        this.gl = data.list
      }else{
        this.alertService.showError("查询供应商列表失败")
      }
    })
}

delete(id:string){
  this.adminService.deleteGysByid(id).then((data:any)=>{
    if(data.msg == "ok"){
      this.alertService.showSuccess("查询供应商列表成功")
      this.getGyses();
    }else{
      this.alertService.showError("查询供应商列表失败")
    }
  })
}
save(){
  const modalwindow= this.modal.create({
    nzTitle: '供应商信息编辑',
    nzContent: GyseditComponent,
    nzViewContainerRef: this.viewContainerRef,
    nzComponentParams: {
      optype:'add'
    },

    nzOnOk:()=>new Promise(resolve => {
      // modalwindow.getContentComponent().save();

      let temp=modalwindow.getContentComponent().g;
      if(!temp.mc){
        this.alertService.showWarning('名称必填');
        resolve(false)
      }else if(!temp.tel){
        this.alertService.showWarning('联系方式必填');
        resolve(false)
      }else if(!temp.lxr){
        this.alertService.showWarning('联系人必填');
        resolve(false)
      }else if(!temp.yyzz){
        this.alertService.showWarning('公司全程必填');
        resolve(false)
      }
      else {
        modalwindow.getContentComponent().save()

        setTimeout(()=>{

          resolve(true);
          this.getGyses();
        }, 1000)
      }


    })
  })
}
update(id:string){
  const modalwindow= this.modal.create({
    nzTitle: '供应商信息编辑',
    nzContent: GyseditComponent,
    nzViewContainerRef: this.viewContainerRef,
    nzComponentParams: {
      optype:'update',
      id:id
    },

    nzOnOk:()=>new Promise(resolve => {
      // modalwindow.getContentComponent().save();

      let temp=modalwindow.getContentComponent().g;
      if(!temp.mc){
        this.alertService.showWarning('名称必填');
        resolve(false)
      }else if(!temp.tel){
        this.alertService.showWarning('联系方式必填');
        resolve(false)
      }else if(!temp.lxr){
        this.alertService.showWarning('联系人必填');
        resolve(false)
      }else if(!temp.yyzz){
        this.alertService.showWarning('公司全程必填');
        resolve(false)
      }
      else {
        modalwindow.getContentComponent().save()

        setTimeout(()=>{

          resolve(true);
          this.getGyses();
        }, 1000)
      }


    })
  })
}

}
